var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json();

var Segment = require('segment'); 
var segment = new Segment();
segment.useDefault();

app.get('/', function(req, res) {
  res.send('This server is running');
});

app.get('/wordsplit', function(req, res) {
  var word = req.query.word;
  console.log('[GET]待分词语句: '+ word);
  var result = segment.doSegment(word, {
    simple: true,
    stripPunctuation: true
  });
  console.log('分词结果: '+ result);
  res.send(result);
});

app.post('/wordsplit', jsonParser, function(req, res) {
  var word = req.body.word;
  console.log('[POST]待分词语句: '+ word);
  var result = segment.doSegment(word, {
    simple: true,
    stripPunctuation: true
  });
  console.log('分词结果: '+ result);
  res.send(result);
})

var server = app.listen(process.env.PORT || 6016, function () {
  var host = server.address().address;
  var port = server.address().port;
  console.log('App listening at http://%s:%s', host, port);
});